import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { api } from '@/lib/apiService';

export async function GET() {
  try {
    // 使用带认证的请求，内部会在服务端通过 getServerSession 获取 token
    const response = await api.auth.get('models');
    return NextResponse.json(response);
  } catch (error) {
    const status = /401/.test(String(error?.message)) ? 401 : 500;
    return NextResponse.json({ error: error.message }, { status });
  }
}

export async function POST(request) {
  try {
    const data = await request.json();
    // 使用带认证的请求
    const response = await api.auth.post('models', data);
    return NextResponse.json(response);
  } catch (error) {
    const status = /401/.test(String(error?.message)) ? 401 : 500;
    return NextResponse.json({ error: error.message }, { status });
  }
}
